package org.idea.spring.dependency.inject.setter;

import org.idea.spring.ioc.bean.Person;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;

import java.util.Map;

/**
 * 基于注解的依赖注入
 *
 * @Author linhao
 * @Date created in 10:53 下午 2021/4/21
 */
public class AnnotationDependencyInjectDemo {

    public static void main(String[] args) {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
        applicationContext.register(AnnotationConfigApplicationContext.class);
        XmlBeanDefinitionReader xmlBeanDefinitionReader = new XmlBeanDefinitionReader(applicationContext);
        xmlBeanDefinitionReader.loadBeanDefinitions("classpath:/META-INF/bean-ioc.xml");
        applicationContext.register(PersonHolder.class);
        applicationContext.refresh();
        Map<String,Person> personMap= applicationContext.getBeansOfType(Person.class);
        for (String key : personMap.keySet()) {
            System.out.println(key);
        }
        PersonHolder personHolder = applicationContext.getBean(PersonHolder.class);
        System.out.println(personHolder);
        applicationContext.close();
    }

    /**
     * 这里需要先对person的bean进行初始化才能注入
     *
     * @param
     * @return
     */
    @Bean
    public PersonHolder personHolder(Person person){
        return new PersonHolder(person);
    }

}
